// Copyright (C) 2024 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
//

#pragma once

#include <kllm/proto/interface.struct.pb.h>
#include <kllm/core/km_context.h>

namespace kllm {

    class KaiChatCompletions {
    public:
        explicit KaiChatCompletions(turbo::Nonnull<KMContext *> c) : _context(c) {}

        ~KaiChatCompletions() = default;

        void chat(const KaiRequest &req, KaiResponse &res);

        void chat_stream(const KaiRequest &req, KaiResponse &res,
                                const std::function<bool(const CompletionsResult &)> &func,
                                const std::function<void()> &on_complete);

        static std::string random_string() {
            static const std::string str("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");

            std::random_device rd;
            std::mt19937 generator(rd());

            std::string result(32, ' ');

            for (int i = 0; i < 32; ++i) {
                result[i] = str[generator() % str.size()];
            }

            return result;
        }

        static std::string gen_chatcmplid() {
            return "chatcmpl-" + random_string();
        }

    private:
        KMContext *_context{nullptr};
    };
}  // namespace kllm
